package com.nineclock.message.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.nineclock.message.config.JPushManager;
import com.nineclock.message.dto.NcMessageDTO;
import com.nineclock.message.pojo.NcMessage;
import com.nineclock.message.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
public class MessageServiceImpl implements MessageService {
    @Autowired
    JPushManager jPushManager;
    @Autowired
    MongoTemplate mongoTemplate;

    /**
     * 1. 推送消息给 管理员
     * 2. 存到mongodb 数据库
     * @param message
     */
    @Override
    public void pushSingleMessage(NcMessage message) throws Exception {
        //System.err.println(message.getContent());
        //1. 推送消息给管理员
        jPushManager.sendNotificationByAlias(message.getTitle(),message.getContent(),message.getTargets(),null);
        //2. 存储到数据库
        Query query = new Query();
        query.addCriteria(Criteria.where("applyUserId").is(message.getApplyUserId()));
        List<NcMessage> messageList = mongoTemplate.find(query, NcMessage.class);
        if (messageList.size() == 0){
            mongoTemplate.save(message);
        }
}

    @Override
    public List<NcMessage> findMessageRecord(NcMessageDTO messageDTO) {
        Query query = new Query();

        if (messageDTO == null){
            messageDTO = new NcMessageDTO();
        }

        /*if (StrUtil.isNotEmpty(messageDTO.getId())){
            query.addCriteria(Criteria.where("_id").is(messageDTO.getId()));
        }
        if (StrUtil.isNotEmpty(messageDTO.getType())){
            query.addCriteria(Criteria.where("messageType").is(messageDTO.getType()));
        }*/
        if (messageDTO.getId() != null){
            query.addCriteria(Criteria.where("_id").is(messageDTO.getId()));
        }
        if (messageDTO.getType() != null){
            query.addCriteria(Criteria.where("messageType").is(messageDTO.getType()));
        }
        if (messageDTO.getTargetId() != null){
            query.addCriteria(Criteria.where("approveUserId").is(messageDTO.getTargetId()));
        }
        if (messageDTO.getUserId() != null){
            query.addCriteria(Criteria.where("applyUserId").is(messageDTO.getUserId()));
        }

        //排序, 根据审核状态升序排序（必须排序，否则会不显示）
        query.with(Sort.by(Sort.Direction.ASC, "approveStatue"));
        //根据条件查询消息
        List<NcMessage> messageList = mongoTemplate.find(query, NcMessage.class);
        return messageList;
    }

    @Override
    public void updateMessageWithApproved(String message) {
        //更新mongodb
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(message));
        Update update = new Update();
        update.set("userStatus",1);
        update.set("approveStatue",1);
        System.err.println("--------------------------------------  " + query+" -----------------------"+ update);
        mongoTemplate.updateFirst(query,update,NcMessage.class);
    }
}
